<?php

use App\Http\Admin\Controller\CommonController;
use App\Util\Helper;
use Plugins\AdminBasic\Enums\BasicSuperConfigEnumType;
use Plugins\AdminBasic\Http\Admin\Controller\SuperConfigController;
use Plugins\AdminBasic\Http\Admin\Service\SuperConfigService;
use Plugins\AdminBasic\Models\BasicSuperConfig;
use Sc\Util\HtmlStructure\Form;
use Sc\Util\HtmlStructure\Form\FormItem;
use Sc\Util\HtmlStructure\Html\Html;

/** @var BasicSuperConfig $superConfig */

$uploadUrl = Helper::route()->to([CommonController::class, 'upload']);
$froalaUploadUrl = Helper::route()->to([CommonController::class, 'froalaUpload']);

Html::create('配置');

$form = Form::create('config')->config('label-width', '120px');

if (Helper::request()->query('_tab')) {
    $form->config('style', 'width:1000px');
}

$supportForm = array_column(SuperConfigService::supportForm(), 'value');

$form->addFormItems(FormItem::hidden('id', 'ID')->default($superConfig->id),);

if ($superConfig->type === BasicSuperConfigEnumType::SingleItemGroup) {
    foreach ($superConfig->config_info as $value) {
        $formType = $value['formType'];
        if (empty($formType) || !in_array($formType, $supportForm)) {
            continue;
        }
        $formType = match ($formType) {
            'image', 'images', 'uploads' => 'upload',
            'selects'                    => 'select',
            default                      => $formType,
        };
        $formItem = FormItem::{$formType}($value['field'], $value['title']);

        if ($value['extra'] && method_exists($formItem, 'options')) {
            $options = \Plugins\AdminBasic\Http\SuperConfig::getSelectOptions($value['extra']);

            $optionsVar = count($value['extra']) == 1
                ? 'System' . substr(md5($value['extra'][0]), 0, 10)
                : $value['field'] . '_options';

            $formItem->setOptionsVarName($optionsVar)->options($options);
        }

        if ($formItem instanceof Form\FormItemUpload) {
            $formItem->setUploadUrl($uploadUrl);
        }

        if ($formItem instanceof Form\FormItemEditor) {
            $formItem->setUploadUrl($froalaUploadUrl);
        }
        if ($value['formType'] === 'image') {
            $formItem->toImage();
        }elseif ($value['formType'] === 'images'){
            $formItem->toImage(true);
        }elseif ($value['formType'] === 'selects' || $value['formType'] === 'uploads'){
            $formItem->multiple();
        }

        $form->addFormItems($formItem);
    }
}else if($superConfig->type === BasicSuperConfigEnumType::Array){
    $form->addFormItems(
        FormItem::table('content')->addItems(
            FormItem::text('value', '配置项')
        )
    );
}else{
    $form->addFormItems(
        FormItem::table('content')->addItems(
            FormItem::text('value', '存储值'),
            FormItem::text('label', '展示值'),
        )
    );
}

$form->addFormItems(
    Helper::request()->query('_tab')
        ? FormItem::submit()->submitUrl(Helper::route()->to([SuperConfigController::class, 'updateBaseConfig'])->url(),)
            ->success("this.\$message.success('更新成功')", true)
        : FormItem::submit()->successClose('parent')->submitUrl(Helper::route()->to([SuperConfigController::class, 'updateBaseConfig'])->url(),
    ),
);

$superConfig->config_value and $form->setData(array_merge(
    $superConfig->config_value,
    [
        'id' => $superConfig->id
    ]
));

return $form->render();